package com.example.system.service.User;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.exception.CommonException;
import com.example.common.exception.enums.CommonExceptionEnum;
import com.example.common.pojo.CommonResult;
import com.example.common.pojo.PageResult;
import com.example.system.DO.UserAddressDO;
import com.example.system.DO.UserDO;
import com.example.system.controller.User.VO.UserLoginReqVO;
import com.example.system.controller.User.VO.UserPageParamReqVO;
import com.example.system.controller.User.VO.UserRespVO;
import com.example.system.mapper.UserMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


import static com.example.common.pojo.CommonResult.*;

@Service
public class UserServiceImp implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public CommonResult<String> doLogin(UserLoginReqVO loginReqVO) {
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getAccount, loginReqVO.getAccount())
                .eq(UserDO::getPassword, loginReqVO.getPassword())
                .last("limit 1");
        UserDO userDO = userMapper.selectOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(userDO)){
            StpUtil.login(userDO.getId(), "PC");
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            StpUtil.getSession().set("loginUser", userDO);
//            String token = JwtUtils.generateToken(userDO.getAccount(), userDO.getId());
            return CommonResult.data(tokenInfo.getTokenValue());
        }else{
            throw new CommonException(CommonExceptionEnum.ERROR401.getCode(),CommonExceptionEnum.ERROR404.getMessage());
        }
    }
    @Override
    public CommonResult<String> doLoginOut() {
        StpUtil.logout();
        return CommonResult.ok();
    }
    @Override
    public PageResult<UserRespVO> getUserPage(UserPageParamReqVO pageParam) {
        MPJLambdaWrapper<UserDO> mpjLambdaWrapper = new MPJLambdaWrapper();
        UserDO userDO = (UserDO)StpUtil.getSession().get("loginUser");

        mpjLambdaWrapper.selectAll(UserDO.class)
                .select(UserAddressDO::getAddress)
                .leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId);
        // 页码 + 数量P
        Page<UserRespVO> tPage = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
        IPage<UserRespVO> page = userMapper.selectJoinPage(tPage, UserRespVO.class, mpjLambdaWrapper);
        return new PageResult<UserRespVO>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<UserRespVO> getUserList() {
        MPJLambdaWrapper<UserDO> mpjLambdaWrapper = new MPJLambdaWrapper();
        mpjLambdaWrapper.selectAll(UserDO.class)
                .select(UserAddressDO::getAddress)
                .leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId);

        return userMapper.selectJoinList(UserRespVO.class, mpjLambdaWrapper);
    }
}
